
package com.starbaba.common;

import com.base.common.database.BaseDao;
import com.base.common.log.LogService;
import com.opensymphony.xwork2.ModelDriven;
import com.starbaba.common.model.*;
import com.starbaba.core.service.CommonConditionService;
import com.starbaba.core.service.impl.CommonConditionServiceImpl;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.utils.json.JsonUtils;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.model.dictionary.Constant;

import java.io.IOException;
import java.util.List;

public class GetQueryCondition extends BaseAction implements ModelDriven<Object>{

	private CommonConditionService commonConditionService;

	public GetQueryCondition(){
		commonConditionService = new CommonConditionServiceImpl();
	}

	
	public void getChannelList(){
		
		BaseDao dao = new BaseDao(Constant.POOL_LIFE);
		PageResponse pageResponse = new PageResponse();
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		
		try {
			String sql = "SELECT DISTINCT channel_code AS channelCode, channel_name AS channelName FROM comm_product_channel";
			List channelList = dao.list(sql, Channel.class);
			pageResponse.setAaData((List)channelList);
			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void getProductList(){
			BaseDao dao = new BaseDao(Constant.POOL_LIFE);
			PageResponse pageResponse = new PageResponse();
			paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		try {
			String sql = " SELECT DISTINCT prdid AS prdid,name AS prdName FROM comm_product_prdid";
			List productList = dao.list(sql, Product.class);
			pageResponse.setAaData((List)productList);
			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void getActivityList(){
		BaseDao dao = new BaseDao(Constant.POOL_LIFE);
		PageResponse pageResponse = new PageResponse();
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		
		try {
			
			String sql = "SELECT DISTINCT id AS activityId, name AS activityName FROM loan_product_info";
			List activityList = dao.list(sql, Activity.class);
			pageResponse.setAaData((List)activityList);
			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void getActivityUnionTypeIsNull(){
		BaseDao dao = new BaseDao(Constant.POOL_LIFE);
		PageResponse pageResponse = new PageResponse();
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		
		try {
			
			String sql = "SELECT DISTINCT id AS activityId, name AS activityName FROM loan_product_info where union_type >0";
			List activityList = dao.list(sql, Activity.class);
			pageResponse.setAaData((List)activityList);
			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void getParentProductList(){
		try {

			List<ParentProduct> parentProducts = commonConditionService.getParentProductList();
			PageResponse pageResponse = new PageResponse();
			pageResponse.setAaData((List)parentProducts);
			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch (IOException e) {
			LogService.error(e);
		}
	}


	public void getChannelGroupList(){
		try {
			List<ChannelGroup> channelGroups = commonConditionService.getChannelGroupList();
			PageResponse pageResponse = new PageResponse();
			pageResponse.setAaData((List)channelGroups);
			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch (IOException e) {
			LogService.error(e);
		}

	}

	public void getSecondaryChannelGroupList(){
		try {
			List<SecondaryChannelGroup> secondaryChannelGroups = commonConditionService.getSecondaryChannelGroupList();
			PageResponse pageResponse = new PageResponse();
			pageResponse.setAaData((List)secondaryChannelGroups);
			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch (IOException e) {
			LogService.error(e);
		}

	}
	
	
	@Override
	public Object getModel() {
		// TODO Auto-generated method stub
		return null;
	}
}
